package datas
{
	import main.ClientConstants;
	import main.DataSubBase;
	import main.LocalStoreInfo;
	
	import struct.SelfPath;
	
	public class ChoosePathData extends DataSubBase
	{
		private var choosePathList:Array;
		
		private var selfPath:Array;
		
		private var store:LocalStoreInfo;
		
		private var currentPath:int = 1;
		
		public function ChoosePathData()
		{
			choosePathList = [];
			
			store = new LocalStoreInfo;
			selfPath = [];
		}
		
		public function element(obj:*):Boolean
		{
			getInfo();
			
			var pathObj:SelfPath;
			
			for(var j:int = 0; j < selfPath.length;j++){
				if(selfPath[j].id == currentPath){
					pathObj = selfPath[j];
				}
			}
			
			var bool:Boolean = false;
			for(var i:int = 0;i < pathObj.info.length;i++){
				for(var k:int = 0;k < pathObj.info[i].length;k++){
					if(pathObj.info[i][k].id == obj.id && pathObj.info[i][k].type == obj.type){
						bool = true;
						break;
					}
				}
			}
			if(bool == false){
				if(pathObj.info[0] && pathObj.info[0] is Array){
					pathObj.info[0].push(obj);
				}else{
					pathObj.info[0] = [];
					pathObj.info[0].push(obj);
				}
			}
			
			return !bool;
		}
		
		public function get selfPathList():Array
		{
			getInfo();
			
			return selfPath;
		}
		
		private function getInfo():void
		{
			if(selfPath.length == 0){
				
				var obj:Object = store.readInfo("selfPath");
				
				if(obj == null){
					var path:SelfPath = new SelfPath;
					
					path.init(data.uri,data.xmlLoaderManager);
					
					path.id = 1;
					path.name = "自定义路线1";
					path.desc = "";
					path.totalDays = 0;
					path.type = ClientConstants.PATH_TYPE_SELF;
					path.info = [];
					path.initWithData(path);
					
					store.storeInfo("currentPathID",path.id);
					
					selfPath.push(path);
				}else{
					var smallObj:Object = new Object;
					smallObj = obj;
					
					selfPath = smallObj as Array;
					
					for(var i:int = 0;i < selfPath.length;i++){
						var path1:SelfPath = new SelfPath;
						path1.init(data.uri,data.xmlLoaderManager);
						
						//						path1.id = selfPath[i].id;
						//						path1.name = selfPath[i].name;
						//						path1.desc = selfPath[i].desc;
						//						path1.totalDays = selfPath[i].totalDays;
						//						path1.type = ClientConstants.PATH_TYPE_SELF;
						//						path1.info = selfPath[i].info;
						//						path1.url = selfPath[i].url;
						
						path1.initWithData(selfPath[i]);
						
						selfPath[i] = path1;
					}
				}
			}
			
		}
		
		public function addANewPath():void
		{	
			var path:SelfPath = new SelfPath;
			path.id = selfPath.length + 1;
			path.name = "自定义路线" + String(int(selfPath.length + 1));
			path.desc = "";
			path.totalDays = 0;
			path.type = ClientConstants.PATH_TYPE_SELF;
			path.info = [];
			
			selfPath.push(path);
			
			saveChoosenPathID(path.id);
		}
		
		public function get currentPathID():int
		{
			var obj:Object = store.readInfo("currentPathID");
			currentPath = int(obj);
			
			if(obj){
				return currentPath;
			}else{
				currentPath = 1;
			}
			
			return currentPath;
		}
		
		public function saveChoosenPathID(num:int):void
		{
			store.storeInfo("currentPathID",Object(num));
			
			this.currentPath = num;
		}
		
		public function saveChange(obj:SelfPath):void
		{
			for(var j:int = 0; j < selfPath.length;j++){
				if(selfPath[j].id == obj.id){
					selfPath[j] = obj;
				}
			}
			
			var smallList:Array = [];
			
			for(var i:int = 0;i < selfPath.length;i++){
				smallList.push(selfPath[i]);
			}
			
			store.storeInfo("selfPath",smallList);
		}
		
		
		public function deltePathAndSave(id:int):void
		{
			for(var i:int = 0;i < selfPath.length;i++){
				if(selfPath[i].id == id){
					selfPath.splice(i,1);
					break;
				}
			}
			
			var smallList:Array = [];
			
			for(var j:int = 0;j < selfPath.length;j++){
				smallList.push(selfPath[j]);
			}
			
			store.storeInfo("selfPath",smallList);
		}
	}
}